app.controller('skuAddCtrl', ['$scope', '$modalInstance','skuService','toaster', function($scope, $modalInstance,skuService,toaster) {
    $scope.skuService=skuService;

    $scope.allowToAddSku=true;

    $scope.addSku=function(){

        if(!$scope.allowToAddSku){
            return;
        }

        $scope.allowToAddSku=false;

        skuService.addSku($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.allowToAddSku=true;
                    toaster.pop('success', "提示", "操作成功");
                    $modalInstance.close("add success");
                    $scope.skuService.sku={};
                    break;
                case 618:
                    $scope.allowToAddSku=true;
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    break;
            }
        });
    }

    $scope.cancel = function () {
        $modalInstance.close("关闭");
    };
}])
;

app.controller('skuDeleteCtrl', ['$scope', '$modalInstance','skuService','data','toaster', function($scope, $modalInstance,skuService,data,toaster) {

    $scope.data=data;

    $scope.allowToDelSku=true;


    $scope.deleteSku=function(){

        if(!$scope.allowToDelSku){
            return;
        }

        $scope.allowToDelSku=false;

        skuService.deleteSku($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.allowToDelSku=true;
                    $modalInstance.close("success");
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default :
                    $modalInstance.close("error");
                    $scope.allowToDelSku=true;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.cancel = function () {
        $modalInstance.close("关闭");
    };
}])
;



app.controller('skuBatchDeleteCtrl', ['$scope', '$modalInstance','skuService','data','toaster', function($scope, $modalInstance,skuService,data,toaster) {

    $scope.data=data;

    $scope.allowToBatchDelSku=true;

    $scope.batchdeleteSku=function(){

        if(!$scope.allowToBatchDelSku){
            return;
        }

        $scope.allowToBatchDelSku=false;

        skuService.batchdeleteSku($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.allowToBatchDelSku=true;
                    $modalInstance.close("success");
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default :
                    $scope.allowToBatchDelSku=true;
                    $modalInstance.close("error");
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.cancel = function () {
        $modalInstance.close("关闭");
    };
}])
;


app.controller('skuEditCtrl', ['$scope', '$modalInstance','skuService','data','toaster', function($scope, $modalInstance,skuService,data,toaster) {
    $scope.data=data;
    $scope.cancel = function () {
        $modalInstance.close("关闭");
    };

    $scope.allowToUpdate=true;

    $scope.update=function(){
        if(!$scope.allowToUpdate){
            return;
        }

        $scope.allowToUpdate=false;

        skuService.update($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.allowToUpdate=true;
                    $modalInstance.close("success");
                    toaster.pop('success', "提示", "操作成功");
                    break;
                case 617:
                case 618:
                    $scope.allowToUpdate=true;
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    $scope.allowToUpdate=true;
                    $modalInstance.close("error");
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;

            }
        });
    }

}]);



app.controller('skuCtrl', ['$scope', '$modal', '$log','utilService','skuService','toaster', function($scope, $modal, $log,utilService,skuService,toaster) {

    //查询该店下的sku分类
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'sku',
            params:{name:$scope.name}
        }).then(function(result){
        });
    }

    $scope.query();



    //打开编辑修改窗口
    $scope.openUpdateSku=function(id,name){

        // query data.
        skuService.queryGoodsAboutSkuCount($scope, id).then(function(result){
            switch (result.status){
                case 200:
                    if(result.data>0){
                        alert("有商品正在使用,无法更改");
                    }else{
                        $scope.openUpdate(id,name);
                    }
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });


    }

    $scope.openUpdate=function(id,name){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/sku/sku_popup_edit.html',
            controller: 'skuEditCtrl',
            size: 'sm',
            scope:$scope,
            resolve:{
                data:function(){
                    return {id:id,name:name};
                }
            }
        });

        modalInstance.result.then(function (result) {
            $scope.query();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    }





    //打开添加弹窗
    $scope.openAddSku = function () {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/sku/sku_popup_add.html',
            controller: 'skuAddCtrl',
            size: 'sm',
            scope:$scope
        });

        modalInstance.result.then(function (result) {
            $scope.query();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };


    //打开删除弹窗
    $scope.openDeleteSku = function (id,name) {
        // query data.
        skuService.queryGoodsAboutSkuCount($scope, id).then(function(result){
            switch (result.status){
                case 200:
                    if(result.data>0){
                        alert("有商品正在使用,无法更改");
                    }else{
                        $scope.openDelete(id,name);
                    }
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    };

    $scope.openDelete=function(id,name){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/sku/sku_popup_delete.html',
            controller: 'skuDeleteCtrl',
            size: 'sm',
            scope:$scope,
            resolve:{
                data:function(){
                    return {id:id,name:name};
                }
            }
        });

        modalInstance.result.then(function (result) {
            $scope.query();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    }


    //打开批量删除弹窗
    $scope.openBatchDeleteSku = function (id,name) {

        if($scope.selected.length<=0){
            alert("请先勾选选项");
            return;
        }


        var modalInstance = $modal.open({
            templateUrl: 'tpl/sku/sku_popup_batch_delete.html',
            controller: 'skuBatchDeleteCtrl',
            size: 'sm',
            scope:$scope,
            resolve:{
                data:function(){
                    return $scope.selected;
                }
            }
        });

        modalInstance.result.then(function (result) {
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });

    };
}])
;